/*******************************************************************************
 * Copyright (c) 2018.
 * This file was written by Fancyears·Milos·Malvis @connor. Any question/bug you can't post to 1243093366@qq.com.
 * ALL RIGHTS RESERVED.
 ******************************************************************************/
package org.fancyears.main;

import static org.fancyears.helper.StringHelper.getRandomChar;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.fancyears.commom.Initializer;

public class FNDialog extends Dialog implements Initializer {

	protected Object result;
	protected Shell shell;
	private ProgressBar progressBar;
	private int i;
	private int index = 0;
	private int Value;
	private int value;
	private Tree tree;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FNDialog(Shell parent, int style) {
		super(parent, style);
		this.shell = parent;
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		final Display display = getParent().getDisplay();
		final int max = progressBar.getMaximum();
		new Thread() {
			public void run() {
					try {
						Thread.sleep(100);
					} catch (Throwable e) {

					}
				display.asyncExec(new Runnable() {
					public void run() {
						for (; i < max; i++) {
							progressBar.setSelection(++index);
							Value = progressBar.getMaximum();
							value = progressBar.getSelection();
							System.out.println("value:" + value);
							createContents();
//							text.setText(value + "%");
							if (Value == value) {
//								final Display dis = Display.getDefault();
//								Shell sh = new Shell(dis);
//								MessageDialog md = new MessageDialog(sh, "信息提示", null, "下载完成", 2, new String[] { "确定" },
//										0);
//								md.open();
//								shell.dispose();
								return;
							}
						}
					}
				});
			}
		}.start();
		shell.open();
		shell.layout();
		while (!shell.isDisposed())

		{
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	public void createContents() {
		Random random = new Random();
		int columns = random.nextInt(10);
		int rows = random.nextInt(10);
		randomCulumns(columns);
		randomItems(rows, columns);

	}

	private void randomItems(int rows, int columns) {
		TreeItem root = new TreeItem(tree, SWT.NONE);
		List<String> ex = new ArrayList<String>();
		Set<TreeItem> set = new HashSet<TreeItem>();

		for (int i = 0; i < columns; i++) {
			ex.add(getRandomChar(3));
		}

		root.setText(ex.toArray(new String[columns]));
		root.setText("Root");
		set.add(root);
		for (int i = 0; i < rows; i++) {
			List<String> exx = new ArrayList<String>();
			for (int j = 0; j < columns; j++) {
				exx.add(getRandomChar(3));
			}
			TreeItem trtmNewTreeitem_4 = new TreeItem(root, SWT.NONE);
			trtmNewTreeitem_4.setText(exx.toArray(new String[columns]));
		}

	}

	private void randomCulumns(int columns) {
		Random random = new Random();
		random.setSeed(26);
		for (i = 0; i < columns; i++) {
			int index = (int) (random.nextFloat() * 10 + 1);
			TreeColumn trclmnNewColumn = new TreeColumn(tree, SWT.NONE);
			trclmnNewColumn.setWidth(100);
			trclmnNewColumn.setText(getRandomChar(index));
		}
	}

	public void run() {
		init();
		show();
	}

	public void show() {
		open();
	}

	public boolean init() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());

		tree = new Tree(shell, SWT.BORDER);
		tree.setHeaderVisible(true);
		tree.setBounds(54, 45, 323, 164);

		progressBar = new ProgressBar(shell, SWT.NONE);
		progressBar.setMaximum(100);
		progressBar.setSelection(0);
		progressBar.setBounds(10, 244, 170, 17);

		return true;
	}
}
